package cn.nexd.app.collector.presenter.fileManager;

import android.content.Context;
import android.os.AsyncTask;

import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import cn.nexd.app.collector.model.ErrorLog;
import cn.nexd.framework.error.NexdCrashException;
import cn.nexd.framework.util.FileUtil;
import cn.nexd.framework.util.SDCardUtil;

/**
 * Created by wangxu on 16/10/9.
 */

public class ErrorFileManagerPresenter implements FileManagerContract.Presenter {

    private FileManagerContract.View iView;
    private Context context;
    private String parentFilePath;
    private String path = SDCardUtil.getSDCardPath() + "/nexd/error/";

    public ErrorFileManagerPresenter(Context context, FileManagerContract.View iView) {
        this.iView = iView;
        this.context = context;
    }

    @Override
    public void loadFile() {
        new LoadFileThread().execute();
    }

    @Override
    public void init() {

    }

    @Override
    public String loadFileContent(String childFileName) {

        File file = new File(parentFilePath + "/" + childFileName);
        String fileContent = "";
        try {
            fileContent = FileUtil.readFile(file);
        } catch (IOException e) {
            e.printStackTrace();
            MobclickAgent.reportError(context, e);
            NexdCrashException.getInstance().reportException(e);
        }
        return fileContent;
    }

    @Override
    public List<String> loadChildFilesName(String fileName) {
        parentFilePath = path + "/" + fileName;
        File parentFile = new File(parentFilePath);
        File[] childFiles = parentFile.listFiles();
        List<String> childFilesName = new ArrayList<>();

        if (childFiles != null) {
            for (File childFile : childFiles) {
                childFilesName.add(childFile.getName());
            }

            Collections.sort(childFilesName, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return o2.compareTo(o1);
                }
            });
        }

        return childFilesName;
    }

    private class LoadFileThread extends AsyncTask {

        // 加载全部数据, 先全部加载,待日后文件多的时候,在处理。
        List<ErrorLog> logs;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            logs = new ArrayList<>();
        }

        @Override
        protected Object doInBackground(Object[] params) {
            File file = new File(path);
            File[] parentFiles = file.listFiles();
            if (parentFiles != null) {
                for (File file1 : parentFiles) {
                    logs.add(new ErrorLog(file1.getName()));
                }

                Collections.sort(logs, new Comparator<ErrorLog>() {
                    @Override
                    public int compare(ErrorLog o1, ErrorLog o2) {
                        return o2.getDate().hashCode() - o1.getDate().hashCode();
                    }
                });
            }

            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            if (logs == null || logs.size() == 0) return;
            iView.updateList(logs);
            super.onPostExecute(o);
        }
    }
}
